package cn.sumpu.app.coolsk.dao.impl;

import java.util.List;
import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;

import cn.sumpu.app.coolsk.dao.DiscussDao;
import cn.sumpu.app.coolsk.domain.Discuss;
import cn.sumpu.app.coolsk.domain.MemberDetail;
import cn.sumpu.app.coolsk.exception.AppBusinessException;

public class DiscussDaoImpl extends SqlSessionDaoSupport implements DiscussDao{
	
	@Override
	public void insert(Discuss discuss)throws AppBusinessException {
		
		try { 
			getSqlSession().insert("cn.sumpu.app.coolsk.persistence.DiscussMapper.insert",discuss);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insert database failure.", e);
			}
			throw new AppBusinessException("insert database failure.", e);
		}
	}

	//求某对象的平均值
	@Override
	public Discuss selectAvgDisByMap(Map<String, Object> map)throws AppBusinessException {
		try {
			Object obj = (Integer) getSqlSession()
					.selectOne("cn.sumpu.app.coolsk.persistence.DiscussMapper.selectAvgDisByMap",
							map);
			return (Discuss) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("selectAvgDisByMap database failure.", e);
			}
			throw new AppBusinessException("selectAvgDisByMap database failure.", e);
		}
	}
	//查询所有的点评
	@SuppressWarnings("unchecked")
	@Override
	public List<Discuss> getAllDiscuss(Map<String, Object> map) throws AppBusinessException {
		try {
		   return getSqlSession().selectList("cn.sumpu.app.coolsk.persistence.DiscussMapper.getAllDiscuss",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getAllDiscuss database failure.", e);
			}
			throw new AppBusinessException("getAllDiscuss database failure.", e);
		}
	}
	//点评总数
	@Override
	public int getAllDiscussCount(Map<String, Object> map)throws AppBusinessException {
		try {
			Object obj = (Integer) getSqlSession()
					.selectOne("cn.sumpu.app.coolsk.persistence.DiscussMapper.getAllDiscussCount",map);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getAllDiscussCount database failure.", e);
			}
			throw new AppBusinessException("getAllDiscussCount database failure.", e);
		}
	}
	//点评详情
	
	@Override
	public Discuss getDiscussById(String id) throws AppBusinessException{
		
		try {
			return (Discuss) getSqlSession().selectOne("cn.sumpu.app.coolsk.persistence.DiscussMapper.getDiscussById",id);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getDiscussById failure.", e);
			}
			throw new AppBusinessException("getDiscussById failure.", e);
		}
	}
	//更新点评
	
	@Override
	public void updateDisById(Map<String, Object> map)throws AppBusinessException {
		
		try { 
			getSqlSession().update("cn.sumpu.app.coolsk.persistence.DiscussMapper.updateDisById", map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateDisById database failure.", e);
			}
			throw new AppBusinessException("updateDisById database failure.", e);
		}
	}
	//
	@SuppressWarnings("unchecked")
	@Override
	public List<Discuss> getDiscussListByObj(Map<String, Object> map) throws AppBusinessException {
		try {
		   return getSqlSession().selectList("cn.sumpu.app.coolsk.persistence.DiscussMapper.getDiscussListByObj",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getDiscussListByObj database failure.", e);
			}
			throw new AppBusinessException("getDiscussListByObj database failure.", e);
		}
	}
	//
	@Override
	public int getDiscussCountByObj(Map<String, Object> map)throws AppBusinessException {
		try {
			Object obj = (Integer) getSqlSession()
					.selectOne("cn.sumpu.app.coolsk.persistence.DiscussMapper.getDiscussCountByObj",map);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getDiscussCountByObj database failure.", e);
			}
			throw new AppBusinessException("getDiscussCountByObj database failure.", e);
		}
	}
	
	
}
